import { defineStore } from "pinia"

export const useTargetStore = defineStore('targetStore', {
    state: () => {
        return {
            targetList: [] // 已选指标
        }
    },
    actions: {
        setTargetList(form) {
            let obj = JSON.parse(JSON.stringify(form)); // 深拷贝防止引用问题
            let existingIndex = this.targetList.findIndex(item => item.searchValue === obj.searchValue);

            if (existingIndex !== -1) {
                // 更新已存在的指标，仅覆盖需要更新的字段
                this.targetList[existingIndex] = {
                    ...this.targetList[existingIndex],
                    ...obj
                };
            } else {
                // 新增指标时，限制最多选择5个
                if (this.targetList.length < 5) {
                    this.targetList.push(obj);
                }
            }
        },
        removeTarget(index) {
            this.targetList.splice(index, 1)
        }
    }
})